package com.swj.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.swj.common.core.constant.CacheConstants;
import com.swj.common.core.model.SysLoginLog;
import com.swj.common.core.model.web.RestResult;
import com.swj.common.core.utils.ResponseUtil;
import com.swj.common.core.utils.poi.ExcelUtil;
import com.swj.common.log.annotation.Log;
import com.swj.common.log.enums.BusinessType;
import com.swj.common.redis.service.IRedisService;
import com.swj.common.security.annotation.RequiresPermissions;
import com.swj.modules.system.model.SysLoginLogQueryVO;
import com.swj.modules.system.service.ISysLoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 系统访问记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/loginLog")
public class SysLoginLogController {
    @Autowired
    private ISysLoginLogService loginLogService;

    @Resource
    private IRedisService redisService;

    @RequiresPermissions("system:logininfor:list")
    @GetMapping("/list")
    public RestResult<List<SysLoginLog>> list(SysLoginLogQueryVO logininfo) {
        List<SysLoginLog> list = loginLogService.selectLogininfoList(logininfo);
        return ResponseUtil.successWithList(list);
    }

    @RequiresPermissions("system:logininfor:list")
    @GetMapping("/page")
    public RestResult<Page<SysLoginLog>> page(SysLoginLogQueryVO logininfo) {
        Page<SysLoginLog> page = loginLogService.selectLogininfoPage(logininfo);
        return ResponseUtil.successWithPageList(page);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginLogQueryVO loginLogQueryVO) {
        List<SysLoginLog> list = loginLogService.selectLogininfoList(loginLogQueryVO);
        ExcelUtil<SysLoginLog> util = new ExcelUtil<SysLoginLog>(SysLoginLog.class);
        util.exportExcel(response, list, "登录日志");
    }

    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public RestResult<?> remove(@PathVariable String[] infoIds) {
        loginLogService.deleteLogininforByIds(infoIds);
        return ResponseUtil.success("删除成功");
    }

    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public RestResult<?> clean() {
        loginLogService.cleanLogininfo();
        return ResponseUtil.success("清空成功");
    }

    @RequiresPermissions("system:logininfor:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public RestResult<?> unlock(@PathVariable("userName") String userName) {
        if (redisService.deleteObject(CacheConstants.PWD_ERR_CNT_KEY + userName)) {
            return ResponseUtil.success("解锁成功");
        } else {
            return ResponseUtil.failWithError("解锁失败", "解锁失败,稍候再尝试");
        }
    }

    @PostMapping("/add")
    public RestResult<Boolean> add(@RequestBody SysLoginLog logininfor) {
        return ResponseUtil.successWithObj(loginLogService.insertLoginLog(logininfor) > 0);
    }
}
